package com.springboot.project.common.service.impl;

import com.springboot.config.tool.ToolUtil;
import com.springboot.project.common.entity.Module;
import com.springboot.project.common.dao.ModuleDao;
import com.springboot.project.common.entity.Rolemodule;
import com.springboot.project.common.node.ModuleNode;
import com.springboot.project.common.node.ZTreeNode;
import com.springboot.project.common.service.ModuleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author lby
 * @since 2018-04-01
 */
@Service
public class ModuleServiceImpl extends ServiceImpl<ModuleDao, Module> implements ModuleService {

    @Override
    public List<ModuleNode> getModuleListByRoles(String roleIdStr) {
        List<Integer> roleIds = ToolUtil.getRoleId(roleIdStr);
        return baseMapper.getModuleListByRoles(roleIds);
    }

    @Override
    public List<String> getPermissionsByRoleId(int roleId) {
        return baseMapper.getPermissionsByRoleId(roleId);
    }

    @Override
    public List<Integer> getMenuIdsByRoleId(Integer roleId) {
        return baseMapper.getMenuIdsByRoleId(roleId);
    }

    @Override
    public List<ZTreeNode> menuTreeList() {
        return baseMapper.menuTreeList();
    }

    @Override
    public List<ZTreeNode> menuTreeListByMenuIds(List<Integer> menuIds) {
        return baseMapper.menuTreeListByMenuIds(menuIds);
    }

    @Override
    public void setAuthority(Integer roleId, String ids) {

        // 删除该角色所有的权限
        baseMapper.deleteByRoleId(roleId);

        // 添加新的权限
        for (Integer menuId : ToolUtil.toIntArray(ids)) {
            baseMapper.insertRelation(roleId,menuId);
        }
    }

    @Override
    public List<Module> list(Map<String, Object> params) {
        List<Module> menus = baseMapper.list(params);
        return menus;
    }

    @Override
    public List<ZTreeNode> pModuleTree() {
        List<ZTreeNode> tree = baseMapper.pModuleTree();
        tree.add(ZTreeNode.createParentByCode());
        return tree;
    }

    @Override
    public List<Module> getListByCondition(String condition) {
        return baseMapper.getListByCondition(condition);
    }
}
